﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace lemon
{
    //扩展方法
    public static class JsonReaderNEx
    {
        //静态调用
        public static T GetValue<T>(this JObject items, string key = null)
        {
            if (items == null) return default(T);
            if (string.IsNullOrWhiteSpace(key)) return items.Value<T>();

            Func<string, T> toDo = null;
            toDo = (k) =>
            {
                if (k.IndexOf(".") == -1)
                {
                    if (items[k] == null) return default(T);
                    return items[k].Value<T>();
                }
                var k1 = k.Substring(0, k.IndexOf("."));
                items = GetValue<JObject>(items, k1);
                if (items == null) return default(T);
                var k2 = k.Substring(k.IndexOf(".") + 1);
                return toDo(k2);
            };
            return toDo(key);
        }

        public static String[] ToStringArry(this JArray items)
        {
            return items.Select(x => x.Value<String>()).ToArray();
        }


        //静态调用
        public static JObject FirstOrDefault(this JArray items, Func<JObject, bool> filter)
        {
            JObject res = null;
            foreach (JObject item in items)
                if (filter(item))
                    return item;
            return res;
        }

        //静态调用
        public static List<JObject> ToList(this JArray items, Func<JObject, bool> filter)
        {
            List<JObject> lst = new List<JObject>();
            foreach (JObject item in items)
                if (filter(item))
                    lst.Add(item);
            return lst;
        }


        //是否包含键
        public static bool HasKey(this JObject json, string key, StringComparison stringComparison = StringComparison.CurrentCultureIgnoreCase)
        {
            var ps = json.Properties();
            foreach (var p in ps)
            {
                if (p.Name.Equals(key, stringComparison)) return true;
            }
            return false;
        }

    }

    public static class JsonReaderN
    {
        //从Json文件中读取对象
        public static JObject GetJObjectFromFile(string filePath, bool isAbsolutePath = false)
        {
            try
            {
                var txt = GetStringFromFile(filePath, isAbsolutePath);
                return JObject.Parse(txt);
            }
            catch
            {
                throw new Exception("读取Json文件失败，文件不存在或者文件内容的格式不是Json，文件路径：" + filePath);
            }
        }

        //读取纯文本
        public static string GetStringFromFile(string filePath, bool isAbsolutePath = false)
        {
            try
            {
                filePath = filePath.Replace("/", "\\");
                var path = filePath;
                path = Path.Combine(Environment.CurrentDirectory, path);

                if (File.Exists(path))
                {
                    var txt = File.ReadAllText(path, Encoding.Default);
                    return txt;
                }
                return null;
            }
            catch
            {
                throw new Exception("读取文件失败，文件不存在，路径：" + filePath);
            }
        }
    }
}
